package com.yaoge.mvc.controller;

import com.yaoge.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * 主要用来测试获取请求参数
 */
@Controller
@RequestMapping("/param")
public class TestRequestParam {

    @RequestMapping("/testServlet")
    public String testServlet(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username +"    password :"+password);
        return "success";
    }

    /**
     * 用来测试自动把请求参数装到形参中,这个需要保证请求参数和 形参名称一致  不然就没有用处
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/testParam1")
    public String testParam1(String username,String password){

        System.out.println("username:"+username +"    password :"+password);
        return "success";
    }

    /**
     * 用来测试@RequstParam 注解 是用来绑定请求参数 和形参的  适用于 请求参数名和形参名不一致的情况
     * @param username
     * @param password
     * @return
     */
        @RequestMapping("/testParam2")
        public String testParam2(@RequestParam("ab") String username,@RequestParam("cd") String password) {

            System.out.println("username:" + username + "    password :" + password);
            return "success";

        }

    /**
     * 测试@RequestHeader  用来获取请求头信息
     * @param host
     * @return
     */
    @RequestMapping("/testHeader")
        public String testHeader(@RequestHeader(value ="host" )String  host) {

            System.out.println("host:"+host );
            return "success";

        }
    @RequestMapping("/testPojo")
        public String testPojo(User user) {

            System.out.println(user );
            return "success";

        }


}
